package jd;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

@WebServlet("/mainServlet")
public class mainServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //  获取配置文件位置
        String configFile = "path.properties";

        Properties properties = new Properties();
        InputStream input = mainServlet.class.getClassLoader().getResourceAsStream(configFile);
        properties.load(input);
        String login = properties.getProperty("login");
        String out = properties.getProperty("out");

        String key = request.getParameter("key");
        switch (key) {
            case "login":
                try {
                    realize(request, response, login);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case "out":
                try {
                    realize(request, response, out);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            default:
                break;
        }
    }

    private void realize(HttpServletRequest request, HttpServletResponse response, String path) throws Exception {
        Class<?> clz = Class.forName(path);
        Action action = (Action) clz.newInstance();
        action.execute(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
